package com.example.hxpc.guidancemanagement.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.activity.ForgetPasswordActivity;
import com.example.hxpc.guidancemanagement.activity.LoginActivity;
import com.example.hxpc.guidancemanagement.activity.MainActivity;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.DialogUtil;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.MD5Util;
import com.example.hxpc.guidancemanagement.tools.SharePreferenceTools;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import java.util.TimerTask;

/**
 * Created by tao on 2017/11/26.
 * 登录页面的Frament
 */

public class LoginFragment extends Fragment implements View.OnClickListener,BaseApi{
    private View rootView;
    private EditText edt_phone , edt_pass;
    private TextView te_forget_pass;
    private Button bu_login , bu_tourist_login;
    private Intent intent;
    private CheckBox checkBox;
    private OkHttpTools okHttpTools = new OkHttpTools();
    private RadioGroup radioGroup;
    private RadioButton radio_bu_admin , radio_bu_con ,radio_bu_sup;
    private String flg = "3";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.frament_login,null);
        }
        initView(rootView);
        return rootView;
    }

    //初始化控件
    public void initView(View rootView){
        radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
        radio_bu_admin = (RadioButton) rootView.findViewById(R.id.admin);
        radio_bu_sup = (RadioButton) rootView.findViewById(R.id.superuser);
        radio_bu_con = (RadioButton) rootView.findViewById(R.id.consumer);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.admin:
                        flg = "1";
                        break;
                    case R.id.superuser:
                        flg = "2";
                        break;
                    case R.id.consumer:
                        flg = "3";
                        break;
                }
            }
        });

        edt_phone = (EditText) rootView.findViewById(R.id.login_phone);
        edt_pass = (EditText) rootView.findViewById(R.id.login_pass);
        te_forget_pass = (TextView) rootView.findViewById(R.id.login_forget_pass);
        te_forget_pass.setOnClickListener(this);
        bu_login = (Button) rootView.findViewById(R.id.login);
        bu_login.setOnClickListener(this);
        bu_tourist_login = (Button) rootView.findViewById(R.id.tourist_login);
        bu_tourist_login.setOnClickListener(this);
//        checkBox = (CheckBox) rootView.findViewById(R.id.check_box);
//        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked){
//                    ToastTools.toastMsg(getActivity(),"已选中");
//                }else {
//                    ToastTools.toastMsg(getActivity(),"没选中");
//                }
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                if (edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"请输入手机号");
                }else if (edt_pass.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"密码不能为空");
                }else{
                    okHttpTools.login(this,getActivity(),edt_phone.getText().toString(), MD5Util.encrypt(edt_pass.getText().toString()),flg);
                }
                break;
            case R.id.tourist_login:
                SharePreferenceTools.getInstance(getActivity()).setString("flg","4");
                intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                ToastTools.toastMsg(getActivity(),"您是游客");
                break;
            case R.id.login_forget_pass:
                Intent intent = new Intent(getActivity(), ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        if (success.equals("true")){
            DialogUtil.show(getActivity(), R.string.logining);
            SharePreferenceTools.getInstance(getActivity()).setString("name",jsonBase.getData().getName());
            SharePreferenceTools.getInstance(getActivity()).setString("phone",jsonBase.getData().getTelephone());
            SharePreferenceTools.getInstance(getActivity()).setString("password",jsonBase.getData().getPassword());
            SharePreferenceTools.getInstance(getActivity()).setString("flg", String.valueOf(jsonBase.getData().getFlg()));
            SharePreferenceTools.getInstance(getActivity()).setString("uid",jsonBase.getData().getUid());
            SharePreferenceTools.getInstance(getActivity()).setString("headPhoto",jsonBase.getData().getHeadPhoto());
            DialogUtil.dismiss2Msg("正在登录");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(getActivity(),MainActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                    ToastTools.toastMsg(getActivity(),"登录成功");
                }
            },2500);
        }else{
            ToastTools.toastMsg(getActivity(),jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
